

module ICarry {

    export class ShellViewModel {

        private _app: ICarryApplication;
        private _view: PrismBreak.View;

        private TopRegion: JQuery;
        private MiddleRegion: JQuery;
        private BottomRegion: JQuery;

        private previousOrientation: number;

        constructor(app: PrismBreak.PbApp) {

            this._app = <ICarryApplication>app;

            this._app.EventManager.Subscribe(PrismBreak.EventType[PrismBreak.EventType.ViewActivated], (view: PrismBreak.View) => {

                this.RefreshMiddleRegionHeight();

            }, 0);

            window.addEventListener(
                "resize",
                () => {

                    //this.CheckAndroidKeyboard();
                    this.CheckOrientation();
                },
                false);

            window.addEventListener(
                "orientationchange",
                () => {
                    this.CheckOrientation();
                },
                false);

        }

        public Initialize( view: PrismBreak.View) {
            ko.applyBindings(this, view.Node);

            this._view = <PrismBreak.View> view;

            this.TopRegion = $(".ui-header");

            if (!this.TopRegion.length) {
                throw new Error("Top region not found");
            }

            this.MiddleRegion = $(".ui-content");

            if (!this.MiddleRegion.length) {
                throw new Error("Middle region not found");
            }

            this.BottomRegion = $(".ui-footer");

            if (!this.BottomRegion.length) {
                throw new Error("Bottom region not found");

            }
        }

        private RefreshMiddleRegionHeight() {
            var windowHeight: number = window.innerHeight;

            this.MiddleRegion.height(windowHeight - this.TopRegion.outerHeight() - this.BottomRegion.outerHeight());
        }

        ////Ios keyboard detection :http://stackoverflow.com/questions/11600040/jquery-js-html5-change-page-content-when-keyboard-is-visible-on-mobile-devices
        //private CheckIosKeyboard(): void {
        //    var newKeyboardState: boolean = $(window).scrollTop() > 0;

        //    //If keyboard was on but now it's not, then check the orientation
        //    if (this._viewModel.IsKeyboardOn.get() !== newKeyboardState) {
        //        this._viewModel.IsKeyboardOn.set(newKeyboardState);
        //    }

        //}

        //private CheckAndroidKeyboard(): void {
        //    //Android keyboard detection 

        //    if (this._app.Config.DeviceType === SolutionConfiguration.DeviceType.Android) {

        //        var newKeyboardState = this._myWindow.innerHeight < this._initialScreenSize;

        //        if (this._viewModel.IsKeyboardOn.get() !== newKeyboardState) {
        //            this._viewModel.IsKeyboardOn.set(newKeyboardState);
        //        }
        //    }
        //}

        private CheckOrientation(): void {
            var myWindow = < any > (window);

            if (typeof (myWindow.orientation) !== "undefined") {

                if (myWindow.orientation !== this.previousOrientation) {
                    this.previousOrientation = myWindow.orientation;

                    this.RefreshMiddleRegionHeight();
                }

                //NO!
                //// (optional) Android doesn't always fire orientationChange on 180 degree turns
                //setInterval(
                //    () => { this.checkOrientation(); },
                //    2000);
            }

            else {
                //window.orientation is not defined in this browser;
                //browser supported: http://caniuse.com/#search=orientation

                this.RefreshMiddleRegionHeight();
            }
        }

    }
} 